home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10299 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Evaluation Requirement Question
  5. Date: Sat, 16 Mar 96 14:45:30 GMT
  6. Organization: none
  7. Distribution: world
  8. Message-ID: <826987530snz@genesis.demon.co.uk>
  9. References: <C23KAB.96Mar15154122@kocrsw25.delcoelect.com>
  10. Reply-To: fred@genesis.demon.co.uk
  11. X-NNTP-Posting-Host: genesis.demon.co.uk
  12. X-Newsreader: Demon Internet Simple News v1.27
  13. X-Mail2News-Path: genesis.demon.co.uk
  14.  
  15. In article <C23KAB.96Mar15154122@kocrsw25.delcoelect.com>
  16.            c23kab@kocrsv08.delcoelect.com "Kirk Bailey" writes:
  17.  
  18. >   In the following example program, notice that the
  19. >test for "i" appears both before and after the call to "foo".
  20. >Note that the printf will only be executed (in this example)
  21. >due to the last "i != 0" check (i is initialized to 0, and
  22. >foo will return 1).  My question:  Is the compiler _required_
  23. >to evaluate "i" twice in all cases where it can not determine
  24. >that the call to "foo" did not alter "i"?
  25.  
  26. The first thing to consider is that main can be called recursively so the
  27. compiler can't assume that when main is called that i will have its
  28. statically initialised value. Global analysis could determine that in this
  29. case assuming the compiler 'knows' about printf but it is extremely unlikely
  30. that a real compiler will do such an analysis.
  31.  
  32. > Are there cases
  33. >where "i" doesn't have to be evaluated twice?  Consider some
  34. >other possible programs:
  35.  
  36. Clearly i won't be evaluated twice if foo() returns zero due to the
  37. short-circuiting properties of ||.
  38.  
  39. A conforming compiler could inline the code of foo within main (there would
  40. still have to be a separate function generated since foo has external linkage).
  41.  
  42. >   1) foo did not _change_ i, or
  43.  
  44. If foo didn't change the value of i then the compiler could determine
  45. that the 2nd "i != 0" has no side-effects, always evaluates to 0 and
  46. hence doesn't have to evaluate it explicitly.
  47.  
  48. >   2) foo did not _change_ i AND foo's definition occurred
  49. >         before its invocation (allowing the compiler to "know"
  50. >         this at the point of invocation), or
  51.  
  52. The compiler can make use of foo's definition whether it occurs before or
  53. after foo. The compiler can do what it likes so long as it doesn't change
  54. the overall behaviour of the program.
  55.  
  56. >   3) "i" was a local variable to main (explicitly init. to
  57. >         zero) - meaning that no function outside of main could
  58. >         alter it through the call to foo.
  59.  
  60. That means that the compiler knows the value of i when the if statement
  61. is entered. Therefore it can determine trivially the result of the first
  62. i == 0 and possible the value of i throughout the entire expression. It might
  63. in that case be able to all 3 of the tests and depending on the
  64. result execute the body of the if statement unconditionally or eliminate
  65. it completely.
  66.  
  67. However what compilers can do in theory and what they do in practice are
  68. entirely different things! It is certainly true however that local variables
  69. are much easier to optimise than global variables. For that reason and others
  70. you should use local variables where possible.
  71.  
  72. >#include <stdio.h>
  73. >
  74. >int i;
  75. >int foo(void);
  76. >
  77. >int main(void)
  78. >{
  79. >  if ( i != 0 || !foo() || i != 0 )
  80. >    printf( "got here!\n" );
  81. >  return 0;
  82. >}
  83. >
  84. >int foo(void)
  85. >{
  86. >  i++;
  87. >  return i;
  88. >}
  89.  
  90. -- 
  91. -----------------------------------------
  92. Lawrence Kirby | fred@genesis.demon.co.uk
  93. Wilts, England | 70734.126@compuserve.com
  94. -----------------------------------------
  95.